package androidx.lifecycle;

import android.annotation.SuppressLint;
import android.app.Application;
import android.os.Bundle;
import androidx.lifecycle.t;
import androidx.savedstate.SavedStateRegistry;
import androidx.savedstate.b;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

public final class r extends t.c {
    private static final Class<?>[] f;
    private static final Class<?>[] g;

    /* renamed from: a  reason: collision with root package name */
    private final Application f756a;

    /* renamed from: b  reason: collision with root package name */
    private final t.a f757b;

    /* renamed from: c  reason: collision with root package name */
    private final Bundle f758c;

    /* renamed from: d  reason: collision with root package name */
    private final e f759d;
    private final SavedStateRegistry e;

    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r1v1, resolved type: java.lang.Class<?>[]} */
    /* JADX DEBUG: Multi-variable search result rejected for TypeSearchVarInfo{r1v2, resolved type: java.lang.Class<?>[]} */
    /* JADX WARNING: Multi-variable type inference failed */
    static {
        /*
            java.lang.Class<androidx.lifecycle.q> r0 = androidx.lifecycle.q.class
            r1 = 2
            java.lang.Class[] r1 = new java.lang.Class[r1]
            java.lang.Class<android.app.Application> r2 = android.app.Application.class
            r3 = 0
            r1[r3] = r2
            r2 = 1
            r1[r2] = r0
            f = r1
            java.lang.Class[] r1 = new java.lang.Class[r2]
            r1[r3] = r0
            g = r1
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: androidx.lifecycle.r.<clinit>():void");
    }

    @SuppressLint({"LambdaLast"})
    public r(Application application, b bVar, Bundle bundle) {
        this.e = bVar.getSavedStateRegistry();
        this.f759d = bVar.getLifecycle();
        this.f758c = bundle;
        this.f756a = application;
        this.f757b = t.a.b(application);
    }

    private static <T> Constructor<T> d(Class<T> cls, Class<?>[] clsArr) {
        for (Constructor<T> constructor : cls.getConstructors()) {
            if (Arrays.equals(clsArr, constructor.getParameterTypes())) {
                return constructor;
            }
        }
        return null;
    }

    public <T extends s> T a(Class<T> cls) {
        String canonicalName = cls.getCanonicalName();
        if (canonicalName != null) {
            return c(canonicalName, cls);
        }
        throw new IllegalArgumentException("Local and anonymous classes can not be ViewModels");
    }

    /* access modifiers changed from: package-private */
    public void b(s sVar) {
        SavedStateHandleController.h(sVar, this.e, this.f759d);
    }

    public <T extends s> T c(String str, Class<T> cls) {
        T newInstance;
        boolean isAssignableFrom = a.class.isAssignableFrom(cls);
        Constructor<T> d2 = isAssignableFrom ? d(cls, f) : d(cls, g);
        if (d2 == null) {
            return this.f757b.a(cls);
        }
        SavedStateHandleController j = SavedStateHandleController.j(this.e, this.f759d, str, this.f758c);
        if (isAssignableFrom) {
            try {
                newInstance = d2.newInstance(new Object[]{this.f756a, j.k()});
            } catch (IllegalAccessException e2) {
                throw new RuntimeException("Failed to access " + cls, e2);
            } catch (InstantiationException e3) {
                throw new RuntimeException("A " + cls + " cannot be instantiated.", e3);
            } catch (InvocationTargetException e4) {
                throw new RuntimeException("An exception happened in constructor of " + cls, e4.getCause());
            }
        } else {
            newInstance = d2.newInstance(new Object[]{j.k()});
        }
        T t = (s) newInstance;
        t.e("androidx.lifecycle.savedstate.vm.tag", j);
        return t;
    }
}
